const Base = require('./base.js');
module.exports = class extends Base {
    /**
     * index action
     * @return {Promise} []
     */
    async indexAction() {
        const page = this.get('page') || 1;
        const size = this.get('size') || 10;
        const sortField = this.get('sortfield') || 'id';
        const shelves_beg = this.get('begin') || '';
        const shelves_end = this.get('end') || '';
        const title = this.get('title') || '';
        let seachdata = { id: ['>', 0]}, timesearch = {};
        if (title) {
            seachdata.title = ['like', `%${title}%`];
        }

        if (shelves_beg || shelves_end) {
            if (shelves_beg) {
              timesearch['egt'] = shelves_beg;
            }
            if (shelves_end) {
              timesearch['elt'] = shelves_end;
            }
            seachdata.update_time = timesearch;
        }

        const model = this.model('active');
        const data = await model.where(
            seachdata
        ).order([sortField]).page(page, size).countSelect();
        for(const item of data.data){
            item.joinnum= await this.model('active_user').where({active_id:item.id}).count('distinct user_id');
        }
        return this.success(data);
    }

    async saveAction() {
        const model = this.model('active');
        let values = this.post();
        let id = this.post('id')||0;
        if (id>0) {
            id=await model.where({ id: id }).update(values);
        } else {
            values['id']=0;
            id = await model.add(values);
            values['id'] = id;
        }
        return this.success(values);
    }
    async updateAction() {
        const model = this.model('active');
        const values = this.post();
        const id = this.post('id');
        let data = await model.where({id: id}).update(values);
        return this.success(data);
    }

    async readAction() {
        const model = this.model('active');
        const id = this.get('id');
        const data=await model.where({id: id}).find();
        return this.success(data);
    }

    async deleteAction() {
        const model = this.model('active');
        const id = this.post('id');
        let data = await model.where({ id: id }).delete();
        return this.success(data);
    }
     /**
     * 任务激活
     */
    async activeAction() {
        const model = this.model('active');
        const values = this.post();
        const id = this.post('id');
        values['is_delete'] = 0;
        let data = await model.where({id: id}).update(values);
        return this.success(data);
    }
    /**
     * 任务冻结
     */
    async freezeAction() {
        const model = this.model('active');
        const id = this.post('id');
        let data = await model.where({ id: id }).update({is_delete:1});
        return this.success(data);
    }

     /**
     * 获取已激活任务
     */
    async getActiveactiveAction() {
        const model = this.model('active');
        let data = await model.where({ is_delete: 0 }).select();
        return this.success(data);
    }


    async joinListAction() {//参与列表
        const model = this.model('active_join');
        const page = this.get('page') || 1;
        const size = this.get('size') || 20;
        const id=this.get('id') || 0;
    const datas = await model.join({ table: 'user',  join: 'left', as:'u', on: ['user_id', 'id'] })
    .where( {active_id:id,type:'join'}).field("u.id as userid, u.name, u.gender,u.city,u.mobile, u.nickname,hiolabs_active_join.is_sign,hiolabs_active_join.update_time").page(page, size).countSelect();
    for(const item of datas.data){
        item.nickname = Buffer.from(item.nickname, 'base64').toString();

    }

        return this.success(datas);
    }

};